import { invoke } from "@tauri-apps/api/core";

/**
 * 创建翻译窗口
 */
export async function createTranslateWindow(): Promise<void> {
  try {
    await invoke("create_translate_window");
  } catch (error) {
    console.error("创建翻译窗口失败:", error);
    throw error;
  }
}

export async function createSettingWindow(): Promise<void> {
  try {
    await invoke("create_setting_window");
  } catch (error) {
    console.error("创建设置窗口失败:", error);
    throw error;
  }
}

export interface ShortcutConfig {
  translate: string;
  screenshot: string;
  setting: string;
}

export async function getShortcuts(): Promise<ShortcutConfig> {
  return await invoke<ShortcutConfig>("get_shortcuts");
}

export async function setShortcuts(cfg: ShortcutConfig): Promise<void> {
  await invoke("set_shortcuts", cfg as any);
}

/**
 * 启动屏幕截图遮罩（全屏透明窗口）
 */
export async function startScreenshotOverlay(): Promise<void> {
  try {
    await invoke("start_screenshot_overlay");
  } catch (error) {
    console.error("启动屏幕截图遮罩失败:", error);
    throw error;
  }
}

/**
 * 关闭遮罩窗口并显示主窗口
 */
export async function closeOverlayWindow(): Promise<void> {
  try {
    await invoke("close_overlay_window");
  } catch (error) {
    console.error("关闭遮罩窗口失败:", error);
    throw error;
  }
}

/**
 * 截图选定的区域
 */
export async function captureSelectedArea(
  x: number,
  y: number,
  width: number,
  height: number
): Promise<string> {
  try {
    return await invoke<string>("capture_selected_area", {
      x,
      y,
      width,
      height,
    });
  } catch (error) {
    console.error("截图失败:", error);
    throw error;
  }
}

/**
 * 屏幕信息
 */
export interface ScreenInfo {
  /** 屏幕名称 */
  name: string;
  /** 屏幕尺寸 [宽度, 高度] */
  size: [number, number];
  /** 屏幕位置 [x, y] */
  position: [number, number];
  /** 缩放因子 */
  scale_factor: number;
}

/**
 * 获取当前鼠标所在屏幕的信息
 *
 * @returns 当前屏幕信息
 *
 * @example
 * ```ts
 * const screen = await getCurrentScreen();
 * console.log('当前屏幕:', screen.name);
 * console.log('屏幕尺寸:', screen.size);
 * console.log('屏幕位置:', screen.position);
 * ```
 */
export async function getCurrentScreen(): Promise<ScreenInfo> {
  try {
    return await invoke<ScreenInfo>("get_current_screen");
  } catch (error) {
    console.error("获取当前屏幕信息失败:", error);
    throw error;
  }
}
